import wangEditor from 'wangeditor'
import {upload} from "@/common/tapi"
export default {
    props:['html'],
    render: function (h) {
        return h('div',{
            attrs:{
                id:'editor'
            },
            style:{
                width:'100%',
                height:'100%'
            }
        })
    },
    mounted(){
        const editor = new wangEditor(`#editor`)
        editor.config.uploadImgMaxLength=1
        editor.config.customUploadImg = function (resultFiles, insertImgFn) {
            const data = new FormData();
            data.append("file", resultFiles[0]);
            data.append("tid", localStorage.getItem('tid'));

            upload(data)
            .then(res=>{
                if(res){
                    insertImgFn(process.env.VUE_APP_HOST+res.data.info.path)
                }
            })
        }
        editor.config.onchange =  newHtml=> {
            this.$emit('changeEdit',newHtml)
        }
        editor.config.colors = [
            '#000000',
            '#eeece0',
            '#1c487f',
            '#4d80bf'
        ]
        editor.create()
        if(this.html){
            editor.txt.html(this.html)
        }
    }
}